package zirao.base.fragment;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import zirao.base.manager.BaseActivityManager;

/**
 * Created by ${lixiaonan} on 2018/7/26.
 */

public abstract class BaseFragment extends Fragment implements View.OnClickListener{

    private int contentViewId;
    public BaseFragment(int contentViewId){
        this.contentViewId = contentViewId;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(this.contentViewId, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        this.initView();
        this.initListener();
        this.initData();
    }

    @Override
    public void onClick(View v) {
        this.widgetClick(v);
    }

    protected abstract void initView();
    protected abstract void initData();

    protected abstract void initListener();
    protected abstract void widgetClick(View v);

    public void showToast(String msg){
        Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
    }

    public void outLog(String Tag, String msg){
        Log.i(Tag,msg);
    }

    /**
     * 页面跳转
     * @param  clz
     * */
    public void openActivity(Class<?> clz) {
        openActivity(clz, null);
    }

    /**
     * 带数据的页面跳转
     * @param  clz
     * @param  bundle
     * */
    public void openActivity(Class<?> clz, Bundle bundle) {
        openActivity(clz, bundle, -1);
    }

    /**
     * 带数据和返回的页面跳转
     * @param  clz
     * @param  bundle
     * */
    public void openActivity(Class<?> clz, Bundle bundle, int requestCode) {
        if(BaseActivityManager.getAppManager().isActivityExist(clz)) return;
        Intent intent = new Intent();
        intent.setClass(getActivity(), clz);
        if(bundle != null){
            intent.putExtras(bundle);
        }
        if(requestCode != -1){
            startActivityForResult(intent,requestCode);
        }else{
            startActivity(intent);
        }
    }

    /**
     * 关闭页面
     * @param  clz
     * */
    public void closeActivity(Class<?> clz) {
        BaseActivityManager.getAppManager().finishActivity(clz);
    }

    /**
     * 关闭页面
     * @param  activity
     * */
    public void closeActivity(Activity activity) {
        BaseActivityManager.getAppManager().finishActivity(activity);
    }

    /**
     * 隐藏软键盘
     * */
    public void hideSoftInput(){
        if(getActivity().getCurrentFocus() != null){
            InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
        }
    }

    /**
     * 显示软键盘
     * */
    public void showInputMethod(){
        if(getActivity().getCurrentFocus() != null){
            InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);
            imm.showSoftInputFromInputMethod(getActivity().getCurrentFocus().getWindowToken(), 0);
        }
    }
}
